﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RTW.Servers;
using RTW.Web.Areas.Query.Models;
using RTW.Web.Comm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using RTW.Models;
using RTW.Core.Extensions;
using RTW.Core.Entity;
using RTW.Core.Enums;

namespace RTW.Web.Areas.Query.Controllers
{
    [Area("Query")]
    public class JiJianController : CommController
    {

        TFilesServer filesServer;
        public JiJianController()
        {
            filesServer = new TFilesServer();
        }
       
        public IActionResult Index()
        {
            return View();
        }


        public async Task<IActionResult> GetJHWenShuJsonData(string filesQueryJson)
        {
            JiJianQueryM query = null;
            if (!string.IsNullOrEmpty(filesQueryJson))
            {
                try
                {
                    query = Newtonsoft.Json.JsonConvert.DeserializeObject<JiJianQueryM>(filesQueryJson);
                }
                catch (Exception ex)
                {

                }
            }
            Expression<Func<TFiles, bool>> predicate = ExpressionBuilder.True<TFiles>();

            predicate = predicate.And(b => b.FileType == 2);
            predicate = predicate.And(b => b.Flag1 == 2);

            predicate = predicate.And(b => b.DMID == UserDMID);

            if (!string.IsNullOrEmpty(query.txtFileNo))
            {
                predicate = predicate.And(b => b.FileNo.Contains(query.txtFileNo));
            }
            //if (!string.IsNullOrEmpty(query.FType))
            //{
            //    predicate = predicate.And(b => b.FType == query.FType);
            //}
            if (!string.IsNullOrEmpty(query.txtStarND))
            {
                predicate = predicate.And(b => int.Parse(b.FYear) >= int.Parse(query.txtStarND));
            }
            if (!string.IsNullOrEmpty(query.txtEndND))
            {
                predicate = predicate.And(b => int.Parse(b.FYear) <= int.Parse(query.txtEndND));
            }
            if (!string.IsNullOrEmpty(query.txtFTrem))
            {
                predicate = predicate.And(b => b.FTerm == query.txtFTrem);
            }
            if (!string.IsNullOrEmpty(query.txtDW))
            {
                predicate = predicate.And(b => b.CV.ResponsiblePerson.Contains(query.txtDW));
            }
            if (!string.IsNullOrEmpty(query.txtAY))
            {
                predicate = predicate.And(b => b.CV.ArticleNo.Contains(query.txtAY));
            }
            if (!string.IsNullOrEmpty(query.txtName))
            {
                predicate = predicate.And(b => b.CV.Title.Contains(query.txtName));
            }
            if (!string.IsNullOrEmpty(query.search))
            {
                predicate = predicate.And(b => b.FileNo.Contains(query.search));
            }
            if (!string.IsNullOrEmpty(query.txtSecondSearch))
            {
                predicate = predicate.And(b => b.FileNo.Contains(query.txtSecondSearch));
            }

            PageInfo pageinfo = (PageInfo)query;
            //a.FYear desc,a.FTerm asc,a.FType asc,right('000000'+ltrim(a.BookNum),7) asc
            List<SortInfo<TFiles, object>> sortInfos = new List<SortInfo<TFiles, object>> {
                   new SortInfo<TFiles, object>{  Orderby=o=>o.FYear, SortMethods = SortEnum.desc },
                   new SortInfo<TFiles, object>{  Orderby=o=>o.FTerm, SortMethods = SortEnum.asc },
                   new SortInfo<TFiles, object>{  Orderby=o=>o.FType, SortMethods = SortEnum.asc },
                   new SortInfo<TFiles, object>{  Orderby=o=>o.BookNum, SortMethods = SortEnum.asc }
            };

            if (!string.IsNullOrEmpty(pageinfo.Sort))
            {
                sortInfos.Clear();
                try
                {
                    // var sort = ExpressionBuilder.GetPropertyInfo(typeof(TFiles), pageinfo.Sort);
                    //PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(TFiles)).Find(pageinfo.Sort);
                    var linq = ExpressionBuilder.CreateExpression<TFiles, object>(pageinfo.Sort);
                    //PropertyInfo prop = typeof(TFiles).GetProperty(pageinfo.Sort);
                    var forder = string.IsNullOrEmpty(pageinfo.Order) || pageinfo.Order == "null" ? SortEnum.asc : (SortEnum)Enum.Parse(typeof(SortEnum), pageinfo.Order);
                    sortInfos.Add(new SortInfo<TFiles, object> { Orderby = linq, SortMethods = forder });
                }
                catch (Exception ex)
                {

                }

            }
            var list = await filesServer.GetJHWenShuList(predicate, sortInfos, pageinfo);

            //var list = await filesServer.GetAsync(m => m.DMID == DMID && m.Flag2 == 1); //string.Format("select * from TOrganization where DMID='{0}' and Flag2=1 order by OrgDepartment asc,Flag1 asc,AddTime asc;", DMID);
            return Json(new { total = list.total, rows = list.rows, search = query.search });
           
        }
    }
}
